package com.cysion.study.ui

import android.content.Intent
import android.net.Uri
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cysion.other.color
import com.cysion.study.R
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.image.RoundTransform
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.darkTextTheme
import com.cysion.study.net.ApiClient
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.HideDataUtils
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import kotlinx.android.synthetic.main.activity_pwithdraw.*

class PWithdrawActivity : BaseActivity(), View.OnClickListener {
    override fun getLayoutId(): Int {
        return R.layout.activity_pwithdraw
    }

    override fun initView() {
        darkTextTheme(color(R.color.white))
        topbar.apply {
            setTitleBackColor(color(R.color.white))
            setTitle("提现")
            setRightImg(R.mipmap.icon_kefu)
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }else  if (pos == TopBar.Pos.RIGHT) {
                    //拨打电话
                    val intent = Intent()
                    intent.action = Intent.ACTION_DIAL//dial是拨号的意思
                    intent.data = Uri.parse("tel:" + "0532-55769987")//这个tel不能改，后面的数字可以随便改
                    startActivity(intent)
                }
            }
        }
        tv_submit.setOnClickListener(this)
        //it.feduMoney  可提现金额

        tv_money.text="可提现金额 ￥ "+UserCache.userInfo?.feduMoney.toString()
        money=UserCache.userInfo?.feduMoney.toString()

    }

    private var money=""
    override fun initData() {
        super.initData()
        getData()
    }

    private fun getData() {
        Alert.loading(this)
        ApiClient.api.familyeduCardinfo(UserCache.token).compose(
            BaseResponseRx.validateToMain()
        )
            ._subscribe {
                _onNext {
                    Alert.close()
                    tv_bank_name.text = it.bank + "(尾号" + HideDataUtils.hideCardNo(it.bankCard) + ")"
                    var bank=it.bank
                    var resourceId = 0
                    if (bank == "招商银行") {
                        resourceId = R.mipmap.icon_zs
                    } else if (bank == "中国银行") {
                        resourceId = R.mipmap.icon_zg
                    } else if (bank == "工商银行") {
                        resourceId = R.mipmap.icon_gs
                    } else if (bank == "农业银行") {
                        resourceId = R.mipmap.icon_ny
                    } else if (bank == "建设银行") {
                        resourceId = R.mipmap.icon_js
                    } else if (bank == "交通银行") {
                        resourceId = R.mipmap.icon_jt
                    } else if (bank == "其他银行") {
                        resourceId = R.mipmap.icon_other
                    }
                    Glide.with(self).load(resourceId)
                        .apply(RequestOptions.placeholderOf(R.mipmap.place_holder_big))
                        .apply(RequestOptions.bitmapTransform(RoundTransform(6)))
                        .into(iv_bank)

                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }

    override fun closeMvp() {
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_submit -> {//提现
                submit()
            }
        }
    }

    private fun submit() {
        var pmoney = et_sex.text.toString().trim()
        et_sex.checkBlank("请输入提现金额")?:return
        if (pmoney.toDouble() <= 0.01) {
            ShowToast("提现金额 必须大于0.01")
            return
        }
        if (pmoney.toDouble() > money.toDouble()) {
            ShowToast("可提现余额不足")
            return
        }
        Alert.loading(self)
        ApiClient.api.withdrawalApply(UserCache.token, et_sex.text.toString().trim())
            .compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    //提现成功
                    UIHelper.jump(self, PWithdrawSuccessActivity::class.java)
//                    ShowToast("提现申请已提交")
                    finish()
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }

}
